/*
   container/heap

   heap 是一个接口, 需要实现 sort/sort 接口和 Push 、Pop方法

   所以总共需要实现的方法有 :
   Push   元素入队
   Pop    元素出队
   Len    获取元素长度
   Less   元素比较
   Swap   交换元素位置

*/

package main

import (
	"container/heap"
	"fmt"
)

// HeapList 实现 heap 接口
type HeapList []int

// Push Method
func (h *HeapList) Push(x interface{}){
	*h = append(*h, x.(int))
}

// Len Method
func (h HeapList) Len() int {
	return len(h)
}

// Less Method
func (h HeapList) Less(i, j int) bool {
	return h[i] < h[j]
}

// Swap Method
func (h HeapList) Swap(i, j int) {
	h[i], h[j] = h[j], h[i]
}

// Pop Method
func (h *HeapList) Pop() interface{}  {
	old := *h
	n := len(old)
	x := old[n -1]
	*h = old[0: n-1]
	return x
}

func main()  {
	intHeap := &HeapList{1,2,3,6}
	fmt.Println(intHeap)
	heap.Init(intHeap)
	heap.Push(intHeap,8)
	fmt.Println(intHeap)
	fmt.Println(heap.Pop(intHeap))
	fmt.Println(intHeap)
}
